package com.codeberry.yws2;

import java.io.*;

public class Response {
  private OutputStream outputStream;
  private boolean prepared;

  public Response(OutputStream outputStream) {
    this.outputStream = outputStream;
  }

  public OutputStream prepareOutputStream(ErrorCode erroCode, String... headers) throws IOException {
    if (!prepared) {
      OutputStreamWriter writer = new OutputStreamWriter(outputStream, "ISO-8859-1");

      writer.write("HTTP/1.0 " + erroCode.getCode() + " " + erroCode.name() + "\r\n");

      for (String header : headers) {
        writer.write(header);
        writer.write("\r\n");
      }

      writer.write("\r\n");
      writer.flush();
      prepared = true;

      return outputStream;
    }

    throw new IllegalStateException("aready prepared");
  }
}
